<script src='/js/editor.js'></script>
<script src='/js/vcode.js'></script>
<script src='/js/contactlist2.js'></script>
<script src='/js/UBBCode.js'></script>
<link href="/style/editor.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="style/sms/style/sms.css" type="text/css" />
<script type='text/javascript'>
function checkForm(form, type){
	var content = content_editor.getHtml();
	if(Prototype.Browser.IE){
		content = content.replace(/\r\n/g, "\n");
	}
	
	if(Prototype.Browser.WebKit){
		content = content.replace(/<div>/g, '\n');
	}
	
	content = content.replace(/<br[\/]?>/g, "\n").stripScripts() ;
	var reg = /<img([^>]*)>/ig;
	var finds = content.match(reg);
	try{
		var reg2 = /src=\"([^\"]*)\"/ig;
		var r = null;
		for(var i=0; i<finds.length; i++){
			if(finds[i].match(/class=[\"\']?face[\"\']?/)){
				reg2 = new RegExp('src=\"([^\"]*)\"', 'ig');
				r = reg2.exec(finds[i]);
				var img = "[img]" + r[1] + "[/img]";
				content = content.replace(finds[i], img);
			}
		}
	}catch(e){
//		alert(e.message);
	}
	content = content.stripScripts().stripTags();
	form.content.value = content;
//	alert(form.content.value);
	var f = false;
	if(type=='send'){
		var f = checkReciever(form.reciever.value);
		if(!f){
			new MessageBox(MessageBox.TYPE_MESSAGE, '您没有填写收件人或收件人格式错误', '', {});
			return false;
		}
	}
	if(form["vcode"]){
		if(form['vcode'].value.trim()==""){
			new MessageBox(MessageBox.TYPE_MESSAGE, '您没有填写验证码', '', {});
			return false;
		}
	}
	content = form.content.value.trim();
//	alert(content);
	if(content.length<2){
		new MessageBox(MessageBox.TYPE_MESSAGE, '内容太短', '', {});
		return false;
	}
	return true;
}

function checkReciever(value){	
	return !(value.trim() == ''|| value.indexOf(',')>=0);
}

function send_sms(form){
	form = $(form);
	disable_submit_btn();
	if(!checkForm(form, 'send')){
		enable_submit_btn();
		return false;
	}
	form.request({
		onComplete: function(transport){
			try{
				var ret = transport.responseText.evalJSON();
				if(ret['result']){
					form.reset();
					content_editor.setContent('');
					location.href = "/?_app=sms&_controller=index&_action=view_inbox&reload=1";
					//new MessageBox(MessageBox.TYPE_MESSAGE, '短信已经发出', '成功', {});
				}else{
					new MessageBox(MessageBox.TYPE_MESSAGE, '短信发送失败（' + (__lang[ret['reason']]?__lang[ret['reason']]:ret['descriptions']) + '）', '失败', {});
					enable_submit_btn();
				}
			}catch(e){}
			//vcode.reload();
		}
	});
	return false;
}

function reply_sms(form){
	form = $(form);
	disable_submit_btn();
	if(!checkForm(form, 'reply')){
		enable_submit_btn();
		return false;
	}
	form.request({
		onComplete: function(transport){
			try{
				var ret = transport.responseText.evalJSON();
				
				if(ret['result']){
					form.reset();
					
					content_editor.setContent('');
					
					var link = (location.href.indexOf('#')>0)?location.href.substr(0, location.href.indexOf('#')):location.href;
					var params = Object.extend(link.toQueryParams(), {reload:1});
					location.href = "/?"+$H(params).toQueryString();
				}else{
					new MessageBox(MessageBox.TYPE_MESSAGE, '短信发送失败（' + __lang[ret['reason']] + '）', '失败', {});
					enable_submit_btn();
				}
			}catch(e){}
//			vcode.reload();
		}
	});
	return false;
}

function delete_message_handler(event){
	event = $E(event);
	var src = Event.element(event);
	var select_All = false;
	if(src.hasClassName('delete-link')){
		Event.stop(event);
		var url = src.getAttribute('href');
		var id = (url.toQueryParams()['id']);
		var onComplete = function(transport){
			try{
				var ret = transport.responseText.evalJSON();
				if(ret.result){
					if(select_All){
						/*$("myMsgList").innerHTML = '<div class="fixpages"><div class="pages"><br/>你的'+mst_name+'是空的</div></div>' ;
						$("myMsgList").show();*/
						location.reload();
						//new Deletion('myMsgList');
					}else{
						new Deletion('row_'+id);
					}
					$("myChecked").innerHTML = 0 ;
				}
			}catch(e){}
		};
		var doAjax = function(){
			new Ajax.Request(url,{
				method: 'post',
				onComplete: onComplete
			});
		};
		var tagsname = src.innerHTML.stripTags();
		if(tagsname=='拒收'){
			new MessageBox(MessageBox.TYPE_OK_CANCEL, '删除此短信您将退出整个对话，您确认删除么', '', {ok: doAjax});
		}
		else if(tagsname=='清空短消息'){
			select_All = true;
			var mst_name = "收件箱";
			var myselect = getNumOfChecked();
			if(myselect<=0){
				new MessageBox(MessageBox.TYPE_MESSAGE, '你的收件箱目前没有任何消息！', '提示','');
				return false;
			} else
				new MessageBox(MessageBox.TYPE_OK_CANCEL, '你将删除收件箱中所有的短消息，您确认删除么', '', {ok: doAjax});
		}
		else if(tagsname=='清空系统通知'){
			select_All = true;
			var mst_name = "收件箱";
			var myselect = getNumOfChecked();
			if(myselect<=0){
				new MessageBox(MessageBox.TYPE_MESSAGE, '你的发件箱目前没有任何消息！', '提示','');
				return false;
			} else{
				new MessageBox(MessageBox.TYPE_OK_CANCEL, '你将删除收件箱中所有的系统通知，您确认删除么', '', {ok: doAjax});
		}
		else if(tagsname=='清空发件箱'){
			select_All = true;
			var mst_name = "发件箱";
			var myselect = getNumOfChecked();
			if(myselect<=0){
				new MessageBox(MessageBox.TYPE_MESSAGE, '你的发件箱目前没有任何消息！', '提示','');
				return false;
			} else{
				new MessageBox(MessageBox.TYPE_OK_CANCEL, '你将删除发件箱中所有的短消息，您确认删除么', '', {ok: doAjax});
			}
			//new MessageBox(MessageBox.TYPE_OK_CANCEL, '你将删除发件箱中所有的短消息，您确认删除么', '', {ok: doAjax});
		}
		else{
			doAjax();
		}
	}
}

function delSmsSelect(app,controller,action){
	var ids = getDelSmsIds();
	var total_del = checkMySelect();
	if(!ids){
		showMsgBox(2,'您还没有选中任何要删除的短信！');
		return false;
	}
	else{
		var url = "http://<?=SNS_DOMAIN?>/?";
		url = url+'_app='+app+"&_controller="+controller+"&_action="+action;
		param = "id="+ids;
		var onLoading = function(){
			showMsgBox(1,'正在删除!');
		};
		var onComplete = function(transport){
			try{
				var ret = transport.responseText.evalJSON();
				if(ret.result){
					showMsgBox(1,'删除成功!');
					var tmpid = ids.split('|');
					var splitid = '';
					if(total_del[0]==total_del[1]){
						//new Deletion('myMsgList');
						var vmsg = '';
						if(action=='view_inbox'){
							vmsg = "收件箱";
						}else if(action=='view_outbox'){
							vmsg = "发件箱";
						}
						/*$("myMsgList").innerHTML = '<div class="fixpages"><div class="pages"><br/>你的'+vmsg+'是空的</div></div>' ;
						$("myMsgList").show();*/
						location.reload(true);
					}else{
						for(var i=0;i<tmpid.length;i++){
							if(tmpid[i].indexOf(':')>0){
								splitid = tmpid[i].split(':');
								if(splitid[1]!="undefined")
									new Deletion('row_'+splitid[1]);
							}
							else{
								new Deletion('row_'+tmpid[i]);
							}
						}
						$("my_msg_box").hide();
						$("myChecked").innerHTML = 0 ;
					}
				}
			} catch(e){}
		};
		var doRequest = function(){
			var request = new Ajax.Request(url,{
				method: 'post',
				parameters: param,
				onLoading: onLoading,
				onComplete: onComplete
			});
		};
		if(total_del[0]==total_del[1]){
			new MessageBox(MessageBox.TYPE_OK_CANCEL, '你将删除所有选定的短信，您确认删除么', '', {ok: doRequest});
		}
		else {
			doRequest();
		}
	}
}
function showMsgBox(type,msg){
	switch(type){
		case 1://短信删除提示
			$("my_msg_box").addClassName("friend-tip");
			break;
		case 2://warning 信息
			$("my_msg_box").addClassName("ufriend-tip");
		default :
			$("my_msg_box").style.display = "none";
	}
	$("my_msg_box").innerHTML = msg;
	$("my_msg_box").show();
}

function getDelSmsIds(){
	var ids = '';
	var chkbox = document.getElementsByName('sms_id');
	var nodes = $A(chkbox);   
  	var sltNodes = nodes.select(function(node)   
   	{   
      return node.checked;   
    });   
   	sltNodes.each(function(node){  
		ids+= node.value+'|';
    });   
    return ids.substring(0,ids.length-1);
} 

function getNumOfChecked(){
	var elements = document.getElementsByTagName("input");
	var selnum=0;
	for(var i=0;i<elements.length;i++){
		if(elements[i].type=="checkbox"){
			++selnum;
		}
	}
	return selnum;
}
function checkMySelect(){
	var elements = document.getElementsByTagName("input");
	var selnum=0,total = 0;
	for(var i=0;i<elements.length;i++){
		if(elements[i].type=="checkbox" ){
			total++;
			if(elements[i].id!="checkall" && elements[i].id!="checkall_1"){
				if(elements[i].checked){
					selnum++;
				}
			}			
		}
	}	
	if(selnum>0 && selnum<total-1){
		$("checkall").checked = false;
	}
	else if(selnum==total-1){
		$("checkall").checked = true;
	}
	else  { 
		$("checkall").checked = false;
	}
	$("myChecked").innerHTML = selnum>=0?selnum:0;
	if(selnum>0 && $("my_msg_box").hasClassName("ufriend-tip"))  $("my_msg_box").hide();
	return new Array(selnum,total-1);
}

function checkAll()
{
     var checkall = document.getElementsByTagName("input");
     var check = document.getElementById("checkall");
     var chack_1 = document.getElementById("checkall_1");
     var k=0;
     for(var i = 0;i < checkall.length; i ++)
     {
        if(checkall[i].type == "checkbox"){
        	if(check.checked==true || chack_1.checked==true){
        		checkall[i].checked = true;
        		k++;
        	}
        	else{
        		checkall[i].checked = false;
        	}
        }
       }
       $("myChecked").innerHTML = k>1?k-1:0;
       if(k>0 && $("my_msg_box").hasClassName("ufriend-tip")) $("my_msg_box").hide();
 }

Event.observe(document, 'click', delete_message_handler);
//Event.observe(document,'change',checkMySelect);
//Event.observe(window, 'load', function(){ Event.observe(document.body, 'click', delete_message_handler); });
</script>